using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace xWinFormsLib
{
    [XmlRoot(Namespace = null, IsNullable = true, ElementName = "Settings")]
    public class Settings
    {
        public List<FormSettings> formSettings = new List<FormSettings>();
        
        /// <summary>
        /// Default constructor
        /// </summary>
        public Settings() { }
        /// <summary>
        /// Save multiple forms
        /// </summary>
        public Settings(List<Form> forms, string filename)
        {
            foreach (Form form in forms)
                AddForm(form);

            Save(filename);
        }
        public Settings(Form form)
        {
            AddForm(form);
        }
        /// <summary>
        /// Save a single form
        /// </summary>
        public Settings(Form form, string filename)
        {
            AddForm(form);
            Save(filename);
        }

        public void AddForm(Form form)
        {
            formSettings.Add(new FormSettings(form));

            FormSettings thisFormSettings = formSettings[formSettings.Count - 1];

            if (form.menu != null)
                thisFormSettings.menuSettings = new MenuSettings(form.menu);

            if (form.Controls != null && form.Controls.Count > 0)
            {
                foreach (Control control in form.Controls.controls)
                {
                    switch (control.Type)
                    {
                        case Control.ControlType.Button:
                            Button button = (Button)control;
                            if (thisFormSettings.buttonList == null)
                                thisFormSettings.buttonList = new List<ButtonSettings>();
                            thisFormSettings.buttonList.Add(new ButtonSettings(button));
                            break;
                        case Control.ControlType.ButtonGroup:
                            ButtonGroup buttonGroup = (ButtonGroup)control;
                            if (thisFormSettings.buttonGroupList == null)
                                thisFormSettings.buttonGroupList = new List<ButtonGroupSettings>();
                            thisFormSettings.buttonGroupList.Add(new ButtonGroupSettings(buttonGroup));
                            break;
                        case Control.ControlType.Checkbox:
                            Checkbox checkbox = (Checkbox)control;
                            if (thisFormSettings.checkboxList == null)
                                thisFormSettings.checkboxList = new List<CheckBoxSettings>();
                            thisFormSettings.checkboxList.Add(new CheckBoxSettings(checkbox));
                            break;
                        case Control.ControlType.CheckboxGroup:
                            CheckboxGroup checkboxGroup = (CheckboxGroup)control;
                            if (thisFormSettings.checkboxGroupList == null)
                                thisFormSettings.checkboxGroupList = new List<CheckboxGroupSettings>();
                            thisFormSettings.checkboxGroupList.Add(new CheckboxGroupSettings(checkboxGroup));
                            break;
                        case Control.ControlType.Combo:
                            Combo combo = (Combo)control;
                            if (thisFormSettings.comboList == null)
                                thisFormSettings.comboList = new List<ComboSettings>();
                            thisFormSettings.comboList.Add(new ComboSettings(combo));
                            break;
                        case Control.ControlType.Label:
                            Label label = (Label)control;
                            if (thisFormSettings.labelList == null)
                                thisFormSettings.labelList = new List<LabelSettings>();
                            thisFormSettings.labelList.Add(new LabelSettings(label));
                            break;
                        case Control.ControlType.Listbox:
                            Listbox listbox = (Listbox)control;
                            if (thisFormSettings.listboxList == null)
                                thisFormSettings.listboxList = new List<ListboxSettings>();
                            thisFormSettings.listboxList.Add(new ListboxSettings(listbox));
                            break;
                        case Control.ControlType.Listview:
                            Listview listview = (Listview)control;
                            if (thisFormSettings.listviewList == null)
                                thisFormSettings.listviewList = new List<ListviewSettings>();
                            thisFormSettings.listviewList.Add(new ListviewSettings(listview));
                            break;
                        case Control.ControlType.NumericUpDown:
                            NumericUpDown numericupdown = (NumericUpDown)control;
                            if (thisFormSettings.numericUpDownList == null)
                                thisFormSettings.numericUpDownList = new List<NumericUpDownSettings>();
                            thisFormSettings.numericUpDownList.Add(new NumericUpDownSettings(numericupdown));
                            break;
                        case Control.ControlType.Potentiometer:
                            Potentiometer potentiometer = (Potentiometer)control;
                            if (thisFormSettings.potentiometerList == null)
                                thisFormSettings.potentiometerList = new List<PotentiometerSettings>();
                            thisFormSettings.potentiometerList.Add(new PotentiometerSettings(potentiometer));
                            break;
                        case Control.ControlType.ProgressBar:
                            Progressbar progressbar = (Progressbar)control;
                            if (thisFormSettings.progressbarList == null)
                                thisFormSettings.progressbarList = new List<ProgressBarSettings>();
                            thisFormSettings.progressbarList.Add(new ProgressBarSettings(progressbar));
                            break;
                        case Control.ControlType.RadioButton:
                            RadioButton radiobutton = (RadioButton)control;
                            if (thisFormSettings.radiobuttonList == null)
                                thisFormSettings.radiobuttonList = new List<RadioButtonSettings>();
                            thisFormSettings.radiobuttonList.Add(new RadioButtonSettings(radiobutton));
                            break;
                        case Control.ControlType.RadioButtonGroup:
                            RadioButtonGroup radiobuttonGroup = (RadioButtonGroup)control;
                            if (thisFormSettings.radiobuttonGroupList == null)
                                thisFormSettings.radiobuttonGroupList = new List<RadioButtonGroupSettings>();
                            thisFormSettings.radiobuttonGroupList.Add(new RadioButtonGroupSettings(radiobuttonGroup));
                            break;
                        case Control.ControlType.Scrollbar:
                            Scrollbar scrollbar = (Scrollbar)control;
                            if (thisFormSettings.scrollbarList == null)
                                thisFormSettings.scrollbarList = new List<ScrollbarSettings>();
                            thisFormSettings.scrollbarList.Add(new ScrollbarSettings(scrollbar));
                            break;
                        case Control.ControlType.Slider:
                            Slider slider = (Slider)control;
                            if (thisFormSettings.sliderList == null)
                                thisFormSettings.sliderList = new List<SliderSettings>();
                            thisFormSettings.sliderList.Add(new SliderSettings(slider));
                            break;
                        case Control.ControlType.Textbox:
                            Textbox textbox = (Textbox)control;
                            if (thisFormSettings.textboxList == null)
                                thisFormSettings.textboxList = new List<TextboxSettings>();
                            thisFormSettings.textboxList.Add(new TextboxSettings(textbox));
                            break;
                        case Control.ControlType.TextButton:
                            TextButton textButton = (TextButton)control;
                            if (thisFormSettings.textbuttonList == null)
                                thisFormSettings.textbuttonList = new List<TextButtonSettings>();
                            thisFormSettings.textbuttonList.Add(new TextButtonSettings(textButton));
                            break;
                    }
                }
            }
        }

        public static void Load(FormCollection formCollection, string filename)
        {
            FileStream stream = null;
            Settings settings = null;

            try
            {
                stream = File.OpenRead(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                settings = (Settings)serializer.Deserialize(stream);
                
                if (settings != null)
                    CreateForms(formCollection, settings);
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception Thrown: " + e.Message);
                if (e.InnerException != null)
                    Console.WriteLine("Inner Exception: " + e.InnerException.Message);

                Console.WriteLine("Error Loading File!");
                //formCollection.Add(new MessageBox(ServiceHelper.Content.Load<SpriteFont>(@"fonts\\tahoma"), "Error loading file."));
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
        }

        private static void CreateForms(FormCollection formCollection, Settings settings)
        {
            foreach (FormSettings formSettings in settings.formSettings)
            {
                Form screenForm = formCollection.Find(formSettings.title);
                if (screenForm != null)
                {
                    if (!screenForm.bVisible)
                        screenForm.Show();
                }
                else
                {
                    Form form = new Form(formSettings.position, formSettings.size, formSettings.title, new Color(formSettings.backcolor), new Color(formSettings.forecolor), formSettings.font, formSettings.alpha, formSettings.movable, formSettings.resizable, formSettings.bCanMinimize, formSettings.bCanMaximize, (Form.BorderStyle)formSettings.borderStyle, Convert.toStyle(formSettings.style), null);

                    if (formSettings.menuSettings != null)
                        form.AddMenu(new Menu(formSettings.menuSettings, form.font, form.style));

                    if (formSettings.buttonList != null && formSettings.buttonList.Count > 0)
                        foreach (ButtonSettings buttonSettings in formSettings.buttonList)
                            form.Add(new Button(buttonSettings.name, buttonSettings.texturename, buttonSettings.position, new Color(buttonSettings.color), form.style));

                    if (formSettings.buttonGroupList != null && formSettings.buttonGroupList.Count > 0)
                    {
                        foreach (ButtonGroupSettings buttonGroupSettings in formSettings.buttonGroupList)
                        {
                            ButtonGroup buttonGroup = new ButtonGroup(buttonGroupSettings.name, buttonGroupSettings.bCanBeNull);
                            foreach (ButtonSettings buttonSettings in buttonGroupSettings.buttonList)
                                buttonGroup.Add(new Button(buttonSettings.name, buttonSettings.texturename, buttonSettings.position, new Color(buttonSettings.color), form.style));
                            form.Add(buttonGroup);
                        }
                    }

                    if (formSettings.checkboxList != null && formSettings.checkboxList.Count > 0)
                        foreach (CheckBoxSettings checkboxSettings in formSettings.checkboxList)
                            form.Add(new Checkbox(checkboxSettings.name, checkboxSettings.position, checkboxSettings.text, checkboxSettings.bChecked, form.font, form.style));

                    if (formSettings.checkboxGroupList != null && formSettings.checkboxGroupList.Count > 0)
                    {
                        foreach (CheckboxGroupSettings checkboxGroupSettings in formSettings.checkboxGroupList)
                        {
                            CheckboxGroup checkboxGroup = new CheckboxGroup(checkboxGroupSettings.name, checkboxGroupSettings.bCanBeNull);
                            foreach (CheckBoxSettings checkboxSettings in checkboxGroupSettings.checkboxList)
                                checkboxGroup.Add(new Checkbox(checkboxSettings.name, checkboxSettings.position, checkboxSettings.text, checkboxSettings.bChecked, form.font, form.style));
                            form.Add(checkboxGroup);
                        }
                    }

                    if (formSettings.comboList != null && formSettings.comboList.Count > 0)
                        foreach (ComboSettings comboSettings in formSettings.comboList)
                        {
                            Combo combo = new Combo(comboSettings.name, comboSettings.position, comboSettings.width, form.font, form.style);
                            combo.Items = comboSettings.items;
                            form.Add(combo);
                        }

                    if (formSettings.labelList != null && formSettings.labelList.Count > 0)
                        foreach (LabelSettings labelSettings in formSettings.labelList)
                            form.Add(new Label(labelSettings.name, labelSettings.position, labelSettings.text, (Label.Alignment)labelSettings.alignment, labelSettings.width, new Color(labelSettings.color), form.font));

                    if (formSettings.listboxList != null && formSettings.listboxList.Count > 0)
                        foreach (ListboxSettings listboxSettings in formSettings.listboxList)
                        {
                            Listbox listbox = new Listbox(listboxSettings.name, listboxSettings.position, listboxSettings.size, new Color(listboxSettings.backcolor), new Color(listboxSettings.forecolor), form.font);
                            foreach (string item in listboxSettings.Items)
                                listbox.AddItem(item);
                            form.Add(listbox);
                        }

                    if (formSettings.listviewList != null && formSettings.listviewList.Count > 0)
                        foreach (ListviewSettings listviewSettings in formSettings.listviewList)
                        {
                            Listview listview = new Listview(listviewSettings.name, listviewSettings.position, listviewSettings.columns, listviewSettings.rows, listviewSettings.columnWidth, listviewSettings.grid, form.font);
                            if (listviewSettings.itemList.list.Count > 0)
                                foreach (ItemArray itemArray in listviewSettings.itemList.list)
                                    listview.Add(itemArray.value, itemArray.subitems);
                            form.Add(listview);
                        }

                    if (formSettings.numericUpDownList != null && formSettings.numericUpDownList.Count > 0)
                        foreach (NumericUpDownSettings numericUpDownSettings in formSettings.numericUpDownList)
                            form.Add(new NumericUpDown(numericUpDownSettings.name, numericUpDownSettings.position, numericUpDownSettings.width, numericUpDownSettings.min, numericUpDownSettings.max, numericUpDownSettings.value, numericUpDownSettings.increment, form.font));

                    if (formSettings.potentiometerList != null && formSettings.potentiometerList.Count > 0)
                        foreach (PotentiometerSettings potentiometerSettings in formSettings.potentiometerList)
                            form.Add(new Potentiometer(potentiometerSettings.name, potentiometerSettings.position, potentiometerSettings.scale, potentiometerSettings.min, potentiometerSettings.max, potentiometerSettings.value, form.style));

                    if (formSettings.progressbarList != null && formSettings.progressbarList.Count > 0)
                        foreach (ProgressBarSettings progressbarSettings in formSettings.progressbarList)
                            form.Add(new Progressbar(progressbarSettings.name, progressbarSettings.position, new Color(progressbarSettings.backgroundColor), new Color(progressbarSettings.foregroundColor), progressbarSettings.width, progressbarSettings.value, progressbarSettings.bContinuous));

                    if (formSettings.radiobuttonList != null && formSettings.radiobuttonList.Count > 0)
                        foreach (RadioButtonSettings radiobuttonSettings in formSettings.radiobuttonList)
                            form.Add(new RadioButton(radiobuttonSettings.name, radiobuttonSettings.position, radiobuttonSettings.text, radiobuttonSettings.bChecked, form.font, form.style));

                    if (formSettings.radiobuttonGroupList != null && formSettings.radiobuttonGroupList.Count > 0)
                    {
                        foreach (RadioButtonGroupSettings radiobuttonGroupSettings in formSettings.radiobuttonGroupList)
                        {
                            RadioButtonGroup radiobuttonGroup = new RadioButtonGroup(radiobuttonGroupSettings.name, radiobuttonGroupSettings.bCanBeNull);
                            foreach (RadioButtonSettings radiobuttonSettings in radiobuttonGroupSettings.radioButtonList)
                                radiobuttonGroup.Add(new RadioButton(radiobuttonSettings.name, radiobuttonSettings.position, radiobuttonSettings.text, radiobuttonSettings.bChecked, form.font, form.style));
                            form.Add(radiobuttonGroup);
                        }
                    }

                    if (formSettings.scrollbarList != null && formSettings.scrollbarList.Count > 0)
                        foreach (ScrollbarSettings scrollbarSettings in formSettings.scrollbarList)
                            form.Add(new Scrollbar(scrollbarSettings.name, scrollbarSettings.position, Convert.toAxis(scrollbarSettings.axis.ToString()), scrollbarSettings.width, scrollbarSettings.height, scrollbarSettings.max, scrollbarSettings.value, form.style));

                    if (formSettings.sliderList != null && formSettings.sliderList.Count > 0)
                        foreach (SliderSettings sliderSettings in formSettings.sliderList)
                            form.Add(new Slider(sliderSettings.name, sliderSettings.position, sliderSettings.width, sliderSettings.min, sliderSettings.max, sliderSettings.value, form.style));

                    if (formSettings.textboxList != null && formSettings.textboxList.Count > 0)
                        foreach (TextboxSettings textboxSettings in formSettings.textboxList)
                            form.Add(new Textbox(textboxSettings.name, textboxSettings.position, textboxSettings.width, textboxSettings.height, textboxSettings.text, textboxSettings.passChar, textboxSettings.maxLength, textboxSettings.bNumerical, textboxSettings.bMultiline, form.font, form.style));

                    if (formSettings.textbuttonList != null && formSettings.textbuttonList.Count > 0)
                        foreach (TextButtonSettings textbuttonSettings in formSettings.textbuttonList)
                            form.Add(new TextButton(textbuttonSettings.name, textbuttonSettings.position, textbuttonSettings.text, textbuttonSettings.width, new Color(textbuttonSettings.color), form.font, form.style));

                    formCollection.forms.Add(form);
                    formCollection.forms[formCollection.forms.Count - 1].Show();
                }
            }
        }

        public void Save(string filename)
        {
            Stream stream = null;
            bool bFileSaved = false;

            try
            {
                if (File.Exists(filename))
                    File.Delete(filename);
                
                stream = File.Create(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(stream, this);
                bFileSaved = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception Thrown: " + e.Message);
                if (e.InnerException != null)
                    Console.WriteLine("Inner Exception: " + e.InnerException.Message);
            }
            finally
            {
                if(bFileSaved)
                    Console.WriteLine("File Saved: " + filename);

                if (stream != null)
                    stream.Close();
            }
        }
    }

    public class FormSettings
    {
        public string name;
        public string title;
        public Vector2 position;
        public Vector2 size;
        public Vector4 backcolor;
        public Vector3 forecolor;
        public float alpha;
        public bool movable;
        public bool resizable;
        public bool bCanMinimize;
        public bool bCanMaximize;
        public int draworder;
        public bool bVisible;
        public string style;
        public string font;
        public BorderStyle borderStyle;

        public List<ButtonSettings> buttonList;
        public List<ButtonGroupSettings> buttonGroupList;
        public List<CheckBoxSettings> checkboxList;
        public List<CheckboxGroupSettings> checkboxGroupList;
        public List<ComboSettings> comboList;
        public List<LabelSettings> labelList;
        public List<ListboxSettings> listboxList;
        public List<ListviewSettings> listviewList;
        public List<NumericUpDownSettings> numericUpDownList;
        public List<PotentiometerSettings> potentiometerList;
        public List<ProgressBarSettings> progressbarList;
        public List<RadioButtonSettings> radiobuttonList;
        public List<RadioButtonGroupSettings> radiobuttonGroupList;
        public List<ScrollbarSettings> scrollbarList;
        public List<SliderSettings> sliderList;
        public List<TextboxSettings> textboxList;
        public List<TextButtonSettings> textbuttonList;
        public MenuSettings menuSettings;

        public FormSettings() { }
        public FormSettings(Form form)
        {
            this.name = form.title;
            this.title = form.title;
            this.position = form.position;
            this.size = form.size;
            this.backcolor = form.backgroundColor.ToVector4();
            this.forecolor = form.foregroundColor.ToVector3();
            this.alpha = form.defaultAlpha;
            this.movable = form.bDraggable;
            this.resizable = form.bResizable;
            this.draworder = form.drawOrder;
            this.bVisible = form.bVisible;
            this.style = form.style.ToString();
            this.font = form.fontName;
            this.bCanMinimize = form.bCanMinimize;
            this.bCanMaximize = form.bCanMaximize;
            this.borderStyle = (BorderStyle)form.borderStyle;
        }
    }

    public class ButtonSettings
    {
        public string name;
        public string texturename;
        public Vector2 position;
        public Vector4 color;

        public ButtonSettings() { }
        public ButtonSettings(Button button)
        {
            this.name = button.name;
            this.position = button.origin;
            this.texturename = button.textureName;
            this.color = button.defalutColor.ToVector4();
        }
    }

    public class ButtonGroupSettings
    {
        public List<ButtonSettings> buttonList;
        public string name;
        public bool bCanBeNull;

        public ButtonGroupSettings() { }
        public ButtonGroupSettings(ButtonGroup buttonGroup)
        {
            this.name = buttonGroup.name;
            this.bCanBeNull = buttonGroup.bCanBeNull;

            buttonList = new List<ButtonSettings>();            

            for (int i = 0; i < buttonGroup.buttons.Count; i++)
                if (buttonGroup.buttons[i] != null)
                    buttonList.Add(new ButtonSettings(buttonGroup.buttons[i]));
        }
    }

    public class CheckBoxSettings
    {
        public string name;
        public Vector2 position;
        public string text;
        public bool bChecked;

        public CheckBoxSettings() { }
        public CheckBoxSettings(Checkbox checkbox)
        {
            this.name = checkbox.name;
            this.position = checkbox.origin;
            this.text = checkbox.text;
            this.bChecked = checkbox.bchecked;
        }
    }

    public class CheckboxGroupSettings
    {
        public List<CheckBoxSettings> checkboxList;
        public string name;
        public bool bCanBeNull;

        public CheckboxGroupSettings() { }
        public CheckboxGroupSettings(CheckboxGroup checkboxGroup)
        {
            this.name = checkboxGroup.name;
            this.bCanBeNull = checkboxGroup.bCanBeNull;
            checkboxList = new List<CheckBoxSettings>();

            for (int i = 0; i < checkboxGroup.checkboxes.Count; i++)
                if (checkboxGroup.checkboxes[i] != null)
                    checkboxList.Add(new CheckBoxSettings(checkboxGroup.checkboxes[i]));
        }
    }

    public class ComboSettings
    {
        public string name;
        public Vector2 position;
        public int width;
        public List<String> items;

        public ComboSettings() { }
        public ComboSettings(Combo combo)
        {
            this.name = combo.name;
            this.position = combo.origin;
            this.width = combo.width;
            this.items = combo.Items;
        }
    }    

    public class LabelSettings
    {
        public string name;
        public Vector2 position;
        public string text;
        public Vector3 color;
        public Alignment alignment;
        public int width;

        public LabelSettings() { }
        public LabelSettings(Label label)
        {
            this.name = label.name;
            this.position = label.origin;
            this.text = label.text;
            this.color = label.color.ToVector3();
            this.alignment = (Alignment)label.alignment;
            this.width = label.width;
        }
    }

    public enum Alignment
    {
        Left,
        Middle,
        Right
    }

    public class ListboxSettings
    {
        public string name;
        public Vector2 position;
        public Point size;
        public Vector3 backcolor;
        public Vector3 forecolor;
        public List<String> Items;

        public ListboxSettings() { }
        public ListboxSettings(Listbox listbox)
        {
            this.name = listbox.name;
            this.position = listbox.origin;
            this.size = listbox.size;
            this.backcolor = listbox.backcolor.ToVector3();
            this.forecolor = listbox.forecolor.ToVector3();
            this.Items = listbox.Items;
        }
    }

    public class ListviewSettings
    {
        public string name;
        public Vector2 position;
        public int columns;
        public int rows;
        public int columnWidth;
        public bool grid;
        public ItemArrayList itemList;

        public ListviewSettings() { }
        public ListviewSettings(Listview listview)
        {
            this.name = listview.name;
            this.position = listview.origin;
            this.columns = listview.columns;
            this.rows = listview.rows;
            this.columnWidth = listview.columnWidth;
            this.grid = listview.bGrid;
            this.itemList = new ItemArrayList(listview.items);
        }
    }

    public class ItemArrayList
    {
        public List<ItemArray> list = new List<ItemArray>();
        public ItemArrayList() { }
        public ItemArrayList(List<Listview.ItemList> items)
        {
            if (items.Count > 0)
            {
                this.list = new List<ItemArray>(items.Count);
                for (int i = 0; i < items.Count; i++)
                    this.list.Add(new ItemArray(items[i].value, items[i].subItems));
            }
        }
    }

    public class ItemArray
    {
        public string value;
        public string[] subitems;
        public ItemArray() { }
        public ItemArray(string value, string[] subitems)
        {
            this.value = value;
            this.subitems = subitems;
        }
    }

    public class MenuSettings
    {
        public Vector2 position;
        public int borderWidth;
        public Vector4 color;
        public List<MenuItemSettings> items;

        public MenuSettings() { }
        public MenuSettings(Menu menu)
        {
            this.position = menu.origin;
            this.borderWidth = menu.borderWidth;
            this.color = menu.color.ToVector4();

            if (menu.items.Count > 0)
            {
                items = new List<MenuItemSettings>(menu.items.Count);
                for (int i = 0; i < menu.items.Count; i++)
                    items.Add(new MenuItemSettings(menu.items[i].name, menu.items[i].subMenu));
            }
        }
    }

    public class MenuItemSettings
    {
        public string name;
        public SubMenuSettings submenuSettings;

        public MenuItemSettings() { }
        public MenuItemSettings(string name, SubMenu submenu)
        {
            this.name = name;
            this.submenuSettings = new SubMenuSettings(submenu);
        }
    }

    public class SubMenuSettings
    {
        public List<SubMenuItem> Items;

        public SubMenuSettings() { }
        public SubMenuSettings(SubMenu submenu)
        {
            if (submenu != null)
            {
                this.Items = new List<SubMenuItem>(submenu.Items.Count);
                for (int i = 0; i < submenu.Items.Count; i++)
                {
                    this.Items.Add(new SubMenuItem(submenu.Items[i].name, submenu.Items[i].subMenu));
                }
            }
        }

    }

    public class SubMenuItem
    {
        public string name;
        public SubMenuSettings subMenu;

        public SubMenuItem() { }
        public SubMenuItem(string name, SubMenu subMenu)
        {
            this.name = name;
            this.subMenu = new SubMenuSettings(subMenu);
        }
    }

    public class NumericUpDownSettings
    {
        public string name;
        public Vector2 position;
        public int width;
        public int min;
        public int max;
        public int value;
        public int increment;

        public NumericUpDownSettings() { }
        public NumericUpDownSettings(NumericUpDown numericUpDown)
        {
            this.name = numericUpDown.name;
            this.position = numericUpDown.origin;
            this.width = numericUpDown.width;
            this.min = numericUpDown.min;
            this.max = numericUpDown.max;
            this.value = numericUpDown.value;
            this.increment = numericUpDown.increment;
        }
    }

    public class PotentiometerSettings
    {
        public string name;
        public Vector2 position;
        public float scale;
        public int min;
        public int max;
        public int value;

        public PotentiometerSettings() { }
        public PotentiometerSettings(Potentiometer potentiometer)
        {
            this.name = potentiometer.name;
            this.position = potentiometer.origin - new Vector2(potentiometer.rect.Width / 2f, potentiometer.rect.Height / 2f);
            this.scale = potentiometer.scale;
            this.min = potentiometer.min;
            this.max = potentiometer.max;
            this.value = potentiometer.value;
        }
    }

    public class ProgressBarSettings
    {
        public string name;
        public Vector2 position;
        public Vector3 backgroundColor;
        public Vector3 foregroundColor;
        public int width;
        public int value;
        public bool bContinuous;

        public ProgressBarSettings() { }
        public ProgressBarSettings(Progressbar progressBar)
        {
            this.name = progressBar.name;
            this.position = progressBar.origin;
            this.backgroundColor = progressBar.backgroundColor.ToVector3();
            this.foregroundColor = progressBar.foregroundColor.ToVector3();
            this.width = progressBar.width;
            this.value = progressBar.value;
            this.bContinuous = progressBar.bContinuous;
        }
    }

    public class RadioButtonSettings
    {
        public string name;
        public Vector2 position;
        public string text;
        public bool bChecked;

        public RadioButtonSettings() { }
        public RadioButtonSettings(RadioButton radioButton)
        {
            this.name = radioButton.name;
            this.position = radioButton.origin;
            this.text = radioButton.text;
            this.bChecked = radioButton.bchecked;
        }
    }

    public class RadioButtonGroupSettings
    {
        public List<RadioButtonSettings> radioButtonList;
        public string name;
        public bool bCanBeNull;

        public RadioButtonGroupSettings() { }
        public RadioButtonGroupSettings(RadioButtonGroup radiobuttonGroup)
        {
            this.name = radiobuttonGroup.name;
            this.bCanBeNull = radiobuttonGroup.bCanBeNull;
            radioButtonList = new List<RadioButtonSettings>();

            for (int i = 0; i < radiobuttonGroup.radioButtons.Count; i++)
                if (radiobuttonGroup.radioButtons[i] != null)
                    radioButtonList.Add(new RadioButtonSettings(radiobuttonGroup.radioButtons[i]));
        }
    }

    public class ScrollbarSettings
    {
        public string name;
        public Vector2 position;
        public Axis axis;
        public int width;
        public int height;
        public int max;
        public int value;

        public ScrollbarSettings() { }
        public ScrollbarSettings(Scrollbar scrollbar)
        {
            this.name = scrollbar.name;
            this.position = scrollbar.origin;
            switch (scrollbar.axis)
            {
                case Scrollbar.Axis.Horizontal:
                    this.axis = Axis.horizontal;
                    this.width = scrollbar.size.X;
                    break;
                case Scrollbar.Axis.Vertical:
                    this.axis = Axis.vertical;
                    this.height = scrollbar.size.Y;
                    break;
            }
        }
    }

    public enum Axis
    {
        vertical,
        horizontal
    }

    public enum BorderStyle
    {
        None,
        FixedSingle
    }

    public class SliderSettings
    {
        public string name;
        public Vector2 position;
        public int width;
        public int min;
        public int max;
        public int value;

        public SliderSettings() { }
        public SliderSettings(Slider slider)
        {
            this.name = slider.name;
            this.position = slider.origin;
            this.width = slider.width;
            this.min = slider.min;
            this.max = slider.max;
            this.value = slider.value;
        }
    }

    public class TextboxSettings
    {
        public string name;
        public Vector2 position;
        public float width;
        public float? height;
        public string text;
        public bool bNumerical;
        public bool bMultiline;
        public int maxLength;
        public string passChar;

        public TextboxSettings() { }
        public TextboxSettings(Textbox textbox)
        {
            this.name = textbox.name;
            this.position = textbox.origin;
            this.width = textbox.width;
            this.height = textbox.height;
            this.text = textbox.text;
            this.bNumerical = textbox.bNumerical;
            this.bMultiline = textbox.bMultiline;
            this.maxLength = textbox.maxLength;
            this.passChar = textbox.passChar;
        }
    }

    public class TextButtonSettings
    {
        public string name;
        public Vector2 position;
        public string text;
        public int width;
        public Vector3 color;

        public TextButtonSettings() { }
        public TextButtonSettings(TextButton textButton)
        {
            this.name = textButton.name;
            this.position = textButton.origin;
            this.text = textButton.text;
            this.width = textButton.width;
            this.color = textButton.color.ToVector3(); 
        }
    }
}
